package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.atguigu.gmall.rabbitmq.config.RabbitConst;
import com.atguigu.gmall.rabbitmq.service.RabbitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther 赵乾坤
 * @Date 2020-12-19 16:12
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;//没有实现类  只有接口

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;//没有实现类  只有接口

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;//没有实现类  只有接口

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;


    //查询所有一级分类的集合
    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    //查询所有2级分类的集合
    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        return baseCategory2Mapper.selectList(new QueryWrapper<BaseCategory2>()
                .eq("category1_id", category1Id));
    }
    //查询所有3级分类的集合
    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
//        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>()
//                .eq("category2_id",category2Id));
        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>()
                .eq("category2_id",category2Id) );
    }

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {

        return baseAttrInfoMapper.attrInfoList(category1Id,category2Id,category3Id);
    }

    //保存平台属性
    @Override
    @Transactional
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        // 什么情况下 是添加，什么情况下是更新，修改 根据baseAttrInfo 的Id
        // baseAttrInfo
        if (baseAttrInfo.getId() != null) {
            // 修改数据
            baseAttrInfoMapper.updateById(baseAttrInfo);
        } else {
            // 1.平台属性表             新增
            // baseAttrInfo 插入数据
            baseAttrInfoMapper.insert(baseAttrInfo);
        }
        // baseAttrValue 平台属性值
        // 修改：通过先删除{baseAttrValue}，在新增的方式！
        // 删除条件：baseAttrValue.attrId = baseAttrInfo.id
        QueryWrapper<BaseAttrValue>  queryWrapper  = new QueryWrapper<>();
        queryWrapper.eq("attr_id", baseAttrInfo.getId());
        baseAttrValueMapper.delete(queryWrapper);

        // 获取页面传递过来的所有平台属性值数据
        //2平台属性值表  兰布达
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        attrValueList.forEach((attrValue)->{//attrValue 自己写的值
            //外键
            attrValue.setAttrId(baseAttrInfo.getId());
            baseAttrValueMapper.insert(attrValue);
        });
    }

    @Override
    public BaseAttrInfo getAttrInfo(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        // 查询到最新的平台属性值集合数据放入平台属性中！
        baseAttrInfo.setAttrValueList(getAttrValueList(attrId));
        return baseAttrInfo;
 
    }


    //查询品牌分页列表：
    @Override
    public IPage<BaseTrademark> baseTrademark(Integer page, Integer limit) {
        //select * from base_tTrademark where  ... limit  page,limit
        return baseTrademarkMapper.selectPage(new Page<BaseTrademark>(page,limit),
                null);
    }

    //@ApiOperation(value = "新增BaseTrademark")
    @Override
    public void baseTrademarkSave(BaseTrademark banner) {
        baseTrademarkMapper.insert(banner);
    }

    //@ApiOperation(value = "删除BaseTrademark")
    @Override
    public void baseTrademarkRemove(Long id) {
        baseTrademarkMapper.deleteById(id);
    }

    //去修改品牌
    @Override
    public BaseTrademark getBaseTrademark(Long id) {

        return baseTrademarkMapper.selectById(id);
    }
    //提交修改品牌
    @Override
    public void baseTrademarkUpdate(BaseTrademark trademark) {

        baseTrademarkMapper.updateById(trademark);
    }

    //商品分页列表
    @Override
    public IPage<SpuInfo> spuList(Integer page, Integer limit, Long category3Id) {
        return spuInfoMapper.selectPage(new Page<SpuInfo>(page,limit),
                new QueryWrapper<SpuInfo>().eq("category3_id",category3Id));

    }

    //查询所有品牌不分页 列表
    @Override
    public List<BaseTrademark> getTrademarkList() {
        return baseTrademarkMapper.selectList(null);
    }

    //查询销售属性字典表
    @Override
    public List<BaseSaleAttr> baseSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }

    /**
     * 根据属性id获取属性值
     * @param attrId
     * @return
     */
    private List<BaseAttrValue> getAttrValueList(Long attrId) {
        // select * from baseAttrValue where attrId = ?
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_id", attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(queryWrapper);
        return baseAttrValueList;
    }

    //保存spu
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1: spu_info  1
        spuInfoMapper.insert(spuInfo);
        //2:spu_image  N
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        spuImageList.forEach(spuImage->{
            //外键
            spuImage.setSpuId(spuInfo.getId());
            spuImageMapper.insert(spuImage);
        });
        //3: spu_sale_attr N
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        spuSaleAttrList.forEach(spuSaleAttr -> {
            //外键
            spuSaleAttr.setSpuId(spuInfo.getId());
            spuSaleAttrMapper.insert(spuSaleAttr);
            //4: spu_sale_value N中N
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.
                    getSpuSaleAttrValueList();
            spuSaleAttrValueList.forEach(spuSaleAttrValue -> {
                //外键
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                //销售属性的名称
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                spuSaleAttrValueMapper.insert(spuSaleAttrValue);
            });
        });
    }

    //根据商品ID查询所有图片
    @Override
    public List<SpuImage> spuImageList(Long spuId) {
        return spuImageMapper.selectList(new QueryWrapper<SpuImage>()
        .eq("spu_id",spuId));

    }

//    根据商品ID查询 销售属性及属性值集合o
    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
      return spuSaleAttrMapper.spuSaleAttrList(spuId);
    }


    //保存SKU
    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        //1:sku_info  1
        skuInfoMapper.insert(skuInfo);
        //2:sku_image N
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        skuImageList.forEach(skuImage->{
            skuImage.setSkuId(skuInfo.getId());
            skuImageMapper.insert(skuImage);
        });
        //3:sku_attr_value N
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        skuAttrValueList.forEach(skuAttrValue -> {
            skuAttrValue.setSkuId(skuInfo.getId());
            skuAttrValueMapper.insert(skuAttrValue);
        });
        //4:sku_sale_attr_value N
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        skuSaleAttrValueList.forEach(skuSaleAttrValue -> {
            // 库存表的外键
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            //商品表的外键
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        });
    }

    //查询SKU分页列表
    @Override
    public IPage<SkuInfo> list(Integer page, Integer limit) {
        return skuInfoMapper.selectPage(new Page<SkuInfo>(page, limit),
                null);
    }

    @Autowired
    private RabbitService rabbitService;

    //上架
    @Override
    public void onSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.ISSALE);//配置  常量  全局的
        skuInfoMapper.updateById(skuInfo);
        // 发消息 给MQ 通知其它微服务 ES
        rabbitService.sendMessage(RabbitConst.EXCHANGE_DIRECT_GOODS,
                RabbitConst.ROUTING_GOODS_UPPER, skuId);
    }
    //下架
    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.CANCELSALE);//配置  常量  全局的
        skuInfoMapper.updateById(skuInfo);
        // 发消息 给MQ 通知其它微服务 ES
        rabbitService.sendMessage(RabbitConst.EXCHANGE_DIRECT_GOODS,
                RabbitConst.ROUTING_GOODS_LOWER, skuId);
    }



    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private  SpuInfoMapper spuInfoMapper;

    @Autowired
    private SpuImageMapper spuImageMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;
    @Autowired
    private SkuInfoMapper skuInfoMapper;
    @Autowired
    private SkuImageMapper skuImageMapper;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;
    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;
}


